package com.iplatform.commons.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;

/**
 * 数据返回处理
 *
 * @author houkunliang
 * @createDate 2019年
 */
public class ResultJson {

	private static final int SUCCESS_CODE = 0;

	private static final int ERR_CODE = 100;

	private static final int SYS_SUCCESS_CODE = 200;

	private static final int SYS_ERR_CODE = 500;

	private static final int LICENSE_ERR_CODE = 101;

	public static String writeSuccess(List<?> lsit) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, "", lsit));
	}

	public static String writeSuccess(List<?> lsit, Page<Object> page) {
		return GosonUtil.getGson().toJson(getResultPageList(SUCCESS_CODE, "", lsit, page));
	}

	public static String writeSuccess(List<?> lsit, String msg) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, msg, lsit));
	}

	public static String writeSuccess(Map<String, ?> map) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, "", map));
	}

	public static String writeSuccess(Map<String, ?> map, String msg) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, msg, map));
	}

	public static String writeSuccess(String msg) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, msg));
	}

	public static String writeSuccess(Object obj) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, "", obj));
	}

	public static String writeSuccess(Object obj, String msg) {
		return GosonUtil.getGson().toJson(getResultMap(SUCCESS_CODE, msg, obj));
	}

	public static String writeErr(Object obj, String msg) {
		return GosonUtil.getGson().toJson(getResultMap(ERR_CODE, msg, obj));
	}

	public static String writeErr(Map<String, ?> map, String msg) {
		return GosonUtil.getGson().toJson(getResultMap(ERR_CODE, msg, map));
	}

	public static String writeErr(String msg) {
		return GosonUtil.getGson().toJson(getResultMap(ERR_CODE, msg));
	}

	public static String writeErrLicense(String msg) {
		return GosonUtil.getGson().toJson(getResultMap(LICENSE_ERR_CODE, msg));
	}

	private static Map<String, Object> getResultMap(int code, String msg, List<?> list) {
		Map<String, Object> rmp = new HashMap<String, Object>();
		rmp.put("code", code);
		rmp.put("codeMsg", msg);
		rmp.put("status", SYS_SUCCESS_CODE);
		rmp.put("dataList", list);
		return rmp;
	}

	private static Map<String, Object> getResultPageList(int code, String msg, List<?> list, Page<Object> page) {
		Map<String, Object> rmp = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("pageNum", page.getPageNum());
		pageMap.put("pageSize", page.getPageSize());
		pageMap.put("startRow", page.getStartRow());
		pageMap.put("endRow", page.getEndRow());
		pageMap.put("total", page.getTotal());
		pageMap.put("pages", page.getPages());
		pageMap.put("reasonable", page.getReasonable());
		pageMap.put("pageSizeZero", page.getPageSizeZero());
		rmp.put("code", code);
		rmp.put("codeMsg", msg);
		rmp.put("status", SYS_SUCCESS_CODE);
		rmp.put("page", pageMap);
		rmp.put("dataList", list);
		return rmp;
	}

	private static Map<String, Object> getResultMap(int code, String msg, Map<?, ?> map) {
		List<Map<?, ?>> list = new ArrayList<Map<?, ?>>();
		list.add(map);
		Map<String, Object> rmp = new HashMap<String, Object>();
		rmp.put("code", code);
		rmp.put("codeMsg", msg);
		rmp.put("status", SYS_SUCCESS_CODE);
		rmp.put("dataList", list);
		return rmp;
	}

	private static Map<String, Object> getResultMap(int code, String msg, Object obj) {
		Map<String, Object> rmp = new HashMap<String, Object>();
		rmp.put("code", code);
		rmp.put("codeMsg", msg);
		rmp.put("status", SYS_SUCCESS_CODE);
		rmp.put("dataList", obj);
		return rmp;
	}

	private static Map<String, Object> getResultMap(int code, String msg) {
		Map<String, Object> rmp = new HashMap<String, Object>();
		rmp.put("code", code);
		rmp.put("status", SYS_SUCCESS_CODE);
		rmp.put("codeMsg", msg);
		return rmp;
	}

	public static Map<String, Object> getSysErrMap(String msg, String sysErrMsg) {
		Map<String, Object> rmp = new HashMap<String, Object>();
		rmp.put("code", ERR_CODE);
		rmp.put("status", SYS_ERR_CODE);
		rmp.put("codeMsg", msg);
		rmp.put("sysErrMsg", sysErrMsg);
		return rmp;
	}

}
